﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FraudMitigator
{
    public partial class EditEntryForm : Form
    {
        static EntryData data;

        public EditEntryForm()
        {
            InitializeComponent();
            editEntryGridView.DataSource = Queries.editEntryFormQuery();

            // change the column headers to properly capitalized text
            foreach (int pos in Strings.editScreenColumnNames.Keys)
            {
                editEntryGridView.Columns[pos].HeaderText = (string)Strings.editScreenColumnNames[pos];
            }
        }

        public void SaveData(EntryData d)
        {
            data = d;
        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            if (!(InputValidator.validateText("name", firstNameTextBox.Text)))
            {
                firstNameTextBox.Select();
                MessageBox.Show("First Name does not have valid data.");
            }
            else if (!(InputValidator.validateText("name", lastNameTextBox.Text)))
            {
                lastNameTextBox.Select();
                MessageBox.Show("Last Name does not have valid data.");
            }
            else if (!(InputValidator.validateText("address", strAddrTextBox.Text)))
            {
                strAddrTextBox.Select();
                MessageBox.Show("Street Address does not have valid data.");
            }
            else if (!(InputValidator.validateText("city", cityTextBox.Text)))
            {
                cityTextBox.Select();
                MessageBox.Show("City does not have valid data.");
            }
            else if (!(InputValidator.validateText("zip", zipTextBox.Text)))
            {
                zipTextBox.Select();
                MessageBox.Show("Zip code invalid.");
            }
            else if (!(InputValidator.validateText("integer", checkNumTextBox.Text)))
            {
                checkNumTextBox.Select();
                MessageBox.Show("Invalid check number.");
            }
            else if (!(InputValidator.validateText("integer", routingNumTextBox.Text)))
            {
                routingNumTextBox.Select();
                MessageBox.Show("Invalid routing number.");
            }
            else if (!(InputValidator.validateText("integer", accountTextBox.Text)))
            {
                accountTextBox.Select();
                MessageBox.Show("Invalid account number.");
            }
            else if (!(InputValidator.validateText("currency", amtTextBox.Text)))
            {
                amtTextBox.Select();
                MessageBox.Show("Invalid amount.");
            }
            else if (!(InputValidator.validateText("state", stateTextBox.Text)))
            {
                stateTextBox.Select();
                MessageBox.Show("Invalid state. Please use only uppercase.");
            }

            //add validation for date here

            else
            {
                data.newFirstName = firstNameTextBox.Text;
                data.newLastName = lastNameTextBox.Text;
                data.newAmount = amtTextBox.Text;
                data.newAddress = strAddrTextBox.Text;
                data.newState = stateTextBox.Text;
                data.newCity = cityTextBox.Text;
                data.newRoutingNum = routingNumTextBox.Text;
                data.newZip = zipTextBox.Text;
                data.newDate = dateTextBox.Text;
                data.newCheckNum = checkNumTextBox.Text;
                data.newAccountNum = accountTextBox.Text;
                data.newLetterSent = letterSentComboBox.Text;
                Queries.updateEditEntryQuery(data);
                editEntryGridView.DataSource = Queries.editEntryFormQuery();

                Strings.Log(MainScreen.user.UserUsername + " performed an edit through the Edit Entries form.");
            }
        }

        private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            if (e.StateChanged == DataGridViewElementStates.Selected)
            {
                EntryData data = new EntryData();
                firstNameTextBox.Text = data.firstName = e.Row.Cells[0].Value.ToString();
                lastNameTextBox.Text = data.lastName = e.Row.Cells[1].Value.ToString();
                strAddrTextBox.Text = data.address = e.Row.Cells[2].Value.ToString();
                cityTextBox.Text = data.city = e.Row.Cells[3].Value.ToString();
                stateTextBox.Text = data.state = e.Row.Cells[4].Value.ToString();
                zipTextBox.Text = data.zip = e.Row.Cells[5].Value.ToString();
                accountTextBox.Text = data.accountNum = e.Row.Cells[6].Value.ToString();
                routingNumTextBox.Text = data.routingNum = e.Row.Cells[7].Value.ToString();
                checkNumTextBox.Text = data.checkNum = e.Row.Cells[8].Value.ToString();
                dateTextBox.Text = data.date = e.Row.Cells[9].Value.ToString();
                amtTextBox.Text = data.amount = e.Row.Cells[10].Value.ToString();
                letterSentComboBox.Text = data.letterSent = e.Row.Cells[11].Value.ToString();

                SaveData(data);
            }
        }

    }

    public struct EntryData
    {
        public string date { get; set; }
        public string lastName { get; set; }
        public string firstName { get; set; }
        public string address { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string zip { get; set; }
        public string amount { get; set; }
        public string checkNum { get; set; }
        public string accountNum { get; set; }
        public string routingNum { get; set; }
        public string letterSent { get; set; }

        public string newDate { get; set; }
        public string newLastName { get; set; }
        public string newFirstName { get; set; }
        public string newAddress { get; set; }
        public string newCity { get; set; }
        public string newState { get; set; }
        public string newZip { get; set; }
        public string newAmount { get; set; }
        public string newCheckNum { get; set; }
        public string newAccountNum { get; set; }
        public string newRoutingNum { get; set; }
        public string newLetterSent { get; set; }
    }
}
